package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.SearchResultAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.SearchBean;
import com.llkj.qingnianguoxue.bean.SearchResultBean;
import com.llkj.qingnianguoxue.customview.flow.FlowLayout;
import com.llkj.qingnianguoxue.customview.flow.TagAdapter;
import com.llkj.qingnianguoxue.customview.flow.TagFlowLayout;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private ImageView ivCancel;//取消
    private TextView tvSearch;
    private EditText etSearch;
    private TagFlowLayout tagHot;
    private List<SearchBean.HotBean> hotData;

    @BindView(R.id.fl_history_title)
    FrameLayout flHistoryTitle;

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvResult;//结果
    private List<SearchResultBean.ResultBean> resultData;
    private SearchResultAdapter resultAdapter;
    private int mCurrentPosition, mDelType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        initView();
        initListener();

        onRefresh();
    }

    private void initView() {
        RelativeLayout rlTop = findViewById(R.id.rl_top);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT, rlTop, R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        ivCancel = (ImageView) findViewById(R.id.iv_cancel);
        tvSearch = findViewById(R.id.tv_search);
        etSearch = (EditText) findViewById(R.id.et_search);
        tagHot = (TagFlowLayout) findViewById(R.id.tag_hot);

        lvResult = (RecyclerView) findViewById(R.id.recyclerView);
        lvResult.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        resultData = new ArrayList<>();
        resultAdapter = new SearchResultAdapter(R.layout.item_search_clear_history, resultData);
        resultAdapter.setType(1);
        lvResult.setAdapter(resultAdapter);
        hotData = new ArrayList<>();
    }

    private void initListener() {
        ivCancel.setOnClickListener(this);
        tvSearch.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);

        lvResult.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (view.getId() == R.id.ivDelHistory) {
                    mCurrentPosition = i;
                    mDelType = 1;
                    delSearchHistory();
                }
            }
        });
        lvResult.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SearchResultBean.ResultBean item = resultData.get(i);
                Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                intent.putExtra("searchWord", item.getSearch());
                startActivity(intent);
            }
        });
    }

    private void delSearchHistory() {
        loadWaitDialog();
        String id = null;
        if (mDelType == 1) {
            SearchResultBean.ResultBean item = resultData.get(mCurrentPosition);
            id = item.getId();
        }
        HttpMethod.delSearch(this, this, id, String.valueOf(mDelType));
    }

    @OnClick({R.id.btn_clear_history})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancel://取消
                finish();
                break;
            case R.id.btn_clear_history://清空历史
                mDelType = 2;
                delSearchHistory();
                break;
            case R.id.tv_search:
                String word = etSearch.getText().toString();
                if (!StringUtils.isEmpty(word)) {
                    if (StringUtils.isContaintSymbol(word)) {
                        ToastUtil.makeLongText(SearchActivity.this, "对不起，请不要输入特殊字符");
                        return;
                    }

                    Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                    intent.putExtra("searchWord", word);
                    startActivity(intent);

                } else {
                    ToastUtil.makeLongText(SearchActivity.this, "请输入搜索关键字");
                }
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (method == HttpMethod.HTTP_INDEX_SEARCH) {
            SearchBean searchBean = GsonUtil.GsonToBean(data, SearchBean.class);
            if (searchBean != null && searchBean.getSearch() != null) {
                hotData.clear();
                SearchBean.HotBean hotBean = null;
                for (int i = 0; i < searchBean.getSearch().size(); i++) {
                    hotBean = new SearchBean.HotBean();
                    hotBean.setTag(searchBean.getSearch().get(i));
                    hotData.add(hotBean);
                }

                tagHot.setAdapter(new TagAdapter<SearchBean.HotBean>(hotData) {
                    @Override
                    public View getView(FlowLayout parent, int position, final SearchBean.HotBean data) {
                        View view = getLayoutInflater().inflate(R.layout.item_search_tag, parent, false);
                        TextView tv = (TextView) view.findViewById(R.id.tv_content);

                        int width = (DisplayUtil.getDensityWdith(SearchActivity.this) - DisplayUtil.dip2px(SearchActivity.this, 50)) / 4;
                        tv.setMinWidth(width);
                        tv.setText(data.getTag());

                        tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                                intent.putExtra("searchWord", data.getTag());
                                startActivity(intent);
                            }
                        });
                        return view;
                    }
                });
                resultData.clear();
                resultData.addAll(searchBean.getUser_search());
                if (resultData.size() > 0) {
                    resultAdapter.notifyDataSetChanged();
                    flHistoryTitle.setVisibility(View.VISIBLE);
                }
//        tagHot.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
//            @Override
//            public boolean onTagClick(View view, int position, FlowLayout parent) {
//
//                return true;
//            }
//        });
            }
        } else if (method == HttpMethod.HTTP_INDEX_DEL_SEARCH) {
            if (mDelType == 1) {
                resultData.remove(mCurrentPosition);
            } else {
                resultData.clear();
            }
            if (resultData.size() == 0) {
                flHistoryTitle.setVisibility(View.GONE);
            }
            resultAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {
        HttpMethod.indexSearch(this, this);
    }
}
